package javaLearning.反射;

import java.io.IOException;
import java.lang.reflect.*;
import java.net.URL;
import java.util.*;

/**
 * @author : K k
 * @date : 21:33 2020/8/19
 */
public class ReflexDemo {

    public static void main(String[] args) {
        try {
            //动态连接
           /* Object o = Proxy.newProxyInstance(ReflexDemo.class.getClassLoader(), ReflexDemo.class.getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    return null;
                }
            });
*/
            Class<?> user1 = ReflexDemo.class.getClassLoader().loadClass("javaLearning.反射.User");//不进行初始化
            User o = (User)user1.newInstance();
            Field name = user1.getDeclaredField("name");
            name.setAccessible(true);
            name.set(o,"lkx");

            System.out.println(o.getName());
            //Class<?> user2 = Class.forName("javaLearning.Reflex.User");//进行初始化
           /* Method[] methods = aClass.getMethods();
            for (Method method:methods){
                System.out.println(method.getName());
            }
            Constructor<?> constructor = user.getConstructor(String.class,int.class);
            User o = (User) constructor.newInstance("lkx",12);
            System.out.println(o.getName());

            Method me = user.getMethod("readBook");
            me.invoke(o, null);

            Vector<User> vector=new Vector<>();
            vector.addElement(o);
            System.out.println(vector.get(0).getName());
            List<User> list=new ArrayList<>();
            list.add(o);
            Iterator<User> iterator = list.iterator();

            ReflexDemo r=new ReflexDemo();
            System.out.println(r.getClass().getClassLoader());
            r.rest();*/
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void rest(){
        Enumeration<URL> eml = null;
        try {
            eml = this.getClass().getClassLoader().getResources("");
        } catch (IOException e) {
            e.printStackTrace();
        }
        while(eml.hasMoreElements()){
            System.out.println("SYSTEMClassLoader Route:  "+eml.nextElement());//系统类加载器的加载路径 是程序运行的当前路径
        }
    }
}
